package com.helen.adapter;

import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.helen.entity.ShoppingCarListEntity;
import com.helen.shopping.R;
import com.helen.utils.ImageLoadUtil;
import com.helen.utils.MyLog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by yang on 2018/5/2.
 */

public class ShoppingCarListAdapter extends BaseQuickAdapter<ShoppingCarListEntity, BaseViewHolder> {
    private Map<String, Integer> pitchOnMap;
    private List<HashMap<String, String>> dataList;
    private RefreshPriceInterface refreshPriceInterface;
    private OnUpdateCarCountListener onUpdateCarCountListener;

    public ShoppingCarListAdapter(int layoutResId, List<ShoppingCarListEntity> data, List<HashMap<String, String>> dataList) {
        super(layoutResId, data);
        this.dataList = dataList;
        pitchOnMap = new HashMap<>();
        for (int i = 0; i < dataList.size(); i++) {
            pitchOnMap.put(dataList.get(i).get("id"), 0);
        }
    }

    @Override
    protected void convert(final BaseViewHolder helper, final ShoppingCarListEntity item) {
        try {
            final CheckBox cbGoods = (CheckBox) helper.getView(R.id.cb_goods);
            ImageView ivGoodsImg = (ImageView) helper.getView(R.id.iv_goods_img);

            TextView tvGoodsName = (TextView) helper.getView(R.id.tv_goods_name);
            TextView tvGoodsSpec = (TextView) helper.getView(R.id.tv_goods_spec);
            TextView tvGoodPrice = (TextView) helper.getView(R.id.tv_good_price);

            final TextView tvGoodsCount = (TextView) helper.getView(R.id.tv_goods_count);
            ImageView ivGoodsMinus = (ImageView) helper.getView(R.id.iv_goods_minus);
            ImageView ivGoodsAdd = (ImageView) helper.getView(R.id.iv_goods_add);

            if (dataList != null && dataList.size() > 0) {
                ImageLoadUtil.displayImage(mContext, ivGoodsImg, item.getProductimg(), R.mipmap.app_logo, R.mipmap.app_logo);
                tvGoodsName.setText(item.getProductname());
                tvGoodsSpec.setText(item.getSpec());
                tvGoodPrice.setText("￥" + item.getPrice());
                tvGoodsCount.setText(item.getCount() + "");


                if (pitchOnMap != null && pitchOnMap.size() > 0) {
                    if (pitchOnMap.get(dataList.get(helper.getAdapterPosition()).get("id")) == 0) {
                        cbGoods.setChecked(false);
                    } else {
                        cbGoods.setChecked(true);
                    }
                } else {
                    cbGoods.setChecked(false);

                }


                cbGoods.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final int index = helper.getAdapterPosition();
                        if (cbGoods.isChecked()) {
                            pitchOnMap.put(dataList.get(index).get("id"), 1);
                        } else {
                            pitchOnMap.put(dataList.get(index).get("id"), 0);
                        }
                        if(refreshPriceInterface!=null){
                            refreshPriceInterface.refreshPrice(pitchOnMap);
//                            setPitchOnMap(pitchOnMap);
                        }

                    }
                });


                ivGoodsMinus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final int index = helper.getAdapterPosition();
                        if (Integer.valueOf(dataList.get(index).get("count")) > 1) {
                            dataList.get(index).put("count", (Integer.valueOf(dataList.get(index).get("count")) - 1) + "");
                            tvGoodsCount.setText(dataList.get(index).get("count") + "");
                            item.setCount(Integer.valueOf(dataList.get(index).get("count")));
                            notifyDataSetChanged();
                            if(refreshPriceInterface!=null){
                                refreshPriceInterface.refreshPrice(pitchOnMap);
                            }

                            if(onUpdateCarCountListener!=null){
                                onUpdateCarCountListener.onCarCountListener(Integer.valueOf(dataList.get(index).get("count")),item.getId());
                            }


                        }


                    }
                });


                ivGoodsAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final int index = helper.getAdapterPosition();
                        dataList.get(index).put("count", (Integer.valueOf(dataList.get(index).get("count")) + 1) + "");
                        tvGoodsCount.setText(dataList.get(index).get("count") + "");
                        item.setCount(Integer.valueOf(dataList.get(index).get("count")));
                        notifyDataSetChanged();
                        if(refreshPriceInterface!=null){
                            refreshPriceInterface.refreshPrice(pitchOnMap);
                        }
                        if(onUpdateCarCountListener!=null){
                            onUpdateCarCountListener.onCarCountListener(Integer.valueOf(dataList.get(index).get("count")),item.getId());
                        }
                    }
                });
            }

        } catch (Exception e) {
            MyLog.e("yang", "购物车列表异常" + e.toString());
        }


    }

    public Map<String, Integer> getPitchOnMap() {
        return pitchOnMap;
    }

    public void setPitchOnMap(Map<String, Integer> pitchOnMap) {
        this.pitchOnMap = new HashMap<>();
        this.pitchOnMap.putAll(pitchOnMap);
    }

    public interface RefreshPriceInterface {
        void refreshPrice(Map<String, Integer> pitchOnMap);
    }


    public void setRefreshPriceInterface(RefreshPriceInterface refreshPriceInterface) {
        this.refreshPriceInterface = refreshPriceInterface;
    }


    public interface OnUpdateCarCountListener {
        void onCarCountListener(int count, int id);
    }

    public void setUpdateCarCountListener(OnUpdateCarCountListener onUpdateCarCountListener) {
        this.onUpdateCarCountListener = onUpdateCarCountListener;
    }
}
